package com.lipy.springootrabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sun.management.Agent;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DLXRabbitMQConfig {

    public static final String DEAD_LETTER_EXCHANGE = "DLX_EXCHANGE";
    public static final String DEAD_LETTER_ROUTE = "DLX_ROUTE";
    public static final String DEAD_LETTER_QUEUE = "DLX_QUEUE";
    public static final String DEAD_LETTER_REDIRECT_ROUTE = "DLX_RE_ROUTE";
    public static final String DEAD_LETTER_REDIRECT_QUEUE = "DLX_RE_QUEUE";

    @Bean
    public DirectExchange deadLetterExchange(){
        return ExchangeBuilder.directExchange(DEAD_LETTER_EXCHANGE).durable(true).build();
    }
    @Bean
    public Queue deadLetterQueue(){

        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        args.put("x-dead-letter-routing-key",DEAD_LETTER_REDIRECT_ROUTE);
        return QueueBuilder.durable(DEAD_LETTER_QUEUE).withArguments(args).build();
    }

    @Bean
    public Binding deadLetterBinding(Queue deadLetterQueue,DirectExchange deadLetterExchange){
        return BindingBuilder.bind(deadLetterQueue).to(deadLetterExchange).with(DEAD_LETTER_ROUTE);
    }

    @Bean("redirectQueue")
    public Queue redirectQueue() {
        return QueueBuilder.durable(DEAD_LETTER_REDIRECT_QUEUE).build();
    }
    @Bean
    public Binding redirectBinding() {
        return new Binding(DEAD_LETTER_REDIRECT_QUEUE, Binding.DestinationType.QUEUE, DEAD_LETTER_EXCHANGE, DEAD_LETTER_REDIRECT_ROUTE, null);
    }
}
